/* Yet Another Forum.NET
 * Copyright (C) 2003-2005 Bjørnar Henden
 * Copyright (C) 2006-2013 Jaben Cargman
 * Copyright (C) 2014-2020 Ingo Herbote
 * https://www.yetanotherforum.net/
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at

 * https://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

namespace YAF.Core.Model
{
    #region Using

    using System;
    using System.Collections.Generic;

    using ServiceStack.OrmLite;

    using YAF.Core.Extensions;
    using YAF.Types;
    using YAF.Types.Flags;
    using YAF.Types.Interfaces.Data;
    using YAF.Types.Models;

    #endregion

    /// <summary>
    ///     The Poll repository extensions.
    /// </summary>
    public static class PollRepositoryExtensions
    {
        #region Public Methods and Operators

        /// <summary>
        /// Get the Poll with all Choices
        /// </summary>
        /// <param name="repository">
        /// The repository.
        /// </param>
        /// <param name="pollId">
        /// The poll id.
        /// </param>
        /// <returns>
        /// The <see cref="List"/>.
        /// </returns>
        public static List<Tuple<Poll, Choice>> GetPollAndChoices(
            this IRepository<Poll> repository,
            [NotNull] int pollId)
        {
            CodeContracts.VerifyNotNull(repository, nameof(repository));

            var expression = OrmLiteConfig.DialectProvider.SqlExpression<Poll>();

            expression.Join<Choice>((p, c) => c.PollID == p.ID)
                .Where<Poll>(p => p.ID == pollId)
                .Select();

            return repository.DbAccess.Execute(db => db.Connection.SelectMulti<Poll, Choice>(expression));
        }

        /// <summary>
        /// Remove Poll
        /// </summary>
        /// <param name="repository">
        /// The repository.
        /// </param>
        /// <param name="pollID">
        /// The poll id. If null all polls in a group a deleted.
        /// </param>
        /// <param name="boardId">
        /// The BoardID id.
        /// </param>
        public static void Remove(
            this IRepository<Poll> repository,
            [NotNull] int pollID,
            [NotNull] int boardId)
        {
            repository.DbFunction.Scalar.poll_remove(
                PollID: pollID,
                BoardID: boardId);
        }

        /// <summary>
        /// Update an existing Poll
        /// </summary>
        /// <param name="repository">
        /// The repository.
        /// </param>
        /// <param name="pollId">
        /// The poll id.
        /// </param>
        /// <param name="question">
        /// The question.
        /// </param>
        /// <param name="closes">
        /// The closes.
        /// </param>
        /// <param name="isClosedBounded">
        /// The is closed bounded.
        /// </param>
        /// <param name="allowMultipleChoices">
        /// The allow multiple choices.
        /// </param>
        /// <param name="showVoters">
        /// The show voters.
        /// </param>
        /// <param name="questionPath">
        /// The question path.
        /// </param>
        public static void Update(
            this IRepository<Poll> repository,
            [NotNull] int pollId,
            [NotNull] string question,
            [CanBeNull] DateTime? closes,
            [NotNull] bool isClosedBounded,
            [NotNull] bool allowMultipleChoices,
            [NotNull] bool showVoters,
            [CanBeNull] string questionPath)
        {
            var flags = new PollFlags
            {
                IsClosedBound = isClosedBounded,
                AllowMultipleChoice = allowMultipleChoices,
                ShowVoters = showVoters,
                AllowSkipVote = false
            };

            repository.UpdateOnly(
                () => new Poll
                {
                    Question = question,
                    Closes = closes,
                    ObjectPath = questionPath,
                    Flags = flags.BitValue
                },
                p => p.ID == pollId);
        }

        /// <summary>
        /// Create new Poll
        /// </summary>
        /// <param name="repository">
        /// The repository.
        /// </param>
        /// <param name="userId">
        /// The user Id.
        /// </param>
        /// <param name="question">
        /// The question.
        /// </param>
        /// <param name="closes">
        /// The closes.
        /// </param>
        /// <param name="isClosedBounded">
        /// The is closed bounded.
        /// </param>
        /// <param name="allowMultipleChoices">
        /// The allow multiple choices.
        /// </param>
        /// <param name="showVoters">
        /// The show voters.
        /// </param>
        /// <param name="questionPath">
        /// The question path.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int Create(
            this IRepository<Poll> repository,
            [NotNull] int userId,
            [NotNull] string question,
            [CanBeNull] DateTime? closes,
            [NotNull] bool isClosedBounded,
            [NotNull] bool allowMultipleChoices,
            [NotNull] bool showVoters,
            [CanBeNull] string questionPath)
        {
            var flags = new PollFlags
            {
                IsClosedBound = isClosedBounded,
                AllowMultipleChoice = allowMultipleChoices,
                ShowVoters = showVoters,
                AllowSkipVote = false
            };

            return repository.Insert(
                 new Poll
                {
                    UserID = userId,
                    Question = question,
                    Closes = closes,
                    ObjectPath = questionPath,
                    Flags = flags.BitValue
                });
        }

        #endregion
    }
}